<!--
 * @Descripttion: 隔离管理
 * @msg1: 统计功能、增删改查隔离人功能
 * @msg2: advanceSearchMixin 为高级搜索代码混入
 * @msg3: 引入组件有 Pagination, TreeSelSearch, IsolationAdd, IsolationEdit
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-12-15 15:09:17
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2022-12-21 11:13:27
-->
<template>
  <div class="common-container">
    <div class="statistics">
      <div class="item">
        <div class="citem">
          <div class="number first">
            {{ isolationPersonTodayCount.inIsolation }}
          </div>
          <span class="title">今日新增</span>
        </div>
        <div class="citem">
          <span class="number first">{{
            isolationPersonTodayCount.sumIsolation
          }}</span>
          <span class="title">今日累计隔离</span>
        </div>
        <div class="citem">
          <span class="number first">{{
            isolationPersonTodayCount.disIsolation
          }}</span>
          <span class="title">今日解除隔离</span>
        </div>
      </div>
      <div class="item">
        <div class="citem">
          <span class="number second">{{
            isolationPersonYesterdayCount.inIsolation
          }}</span>
          <span class="title">昨日新增</span>
        </div>
        <div class="citem">
          <span class="number second">{{
            isolationPersonYesterdayCount.sumIsolation
          }}</span>
          <span class="title">昨日累计隔离</span>
        </div>
        <div class="citem">
          <span class="number second">{{
            isolationPersonYesterdayCount.disIsolation
          }}</span>
          <span class="title">昨日解除隔离</span>
        </div>
      </div>
    </div>

    <div class="header">
      <div class="header_left">
        <el-input v-model="searchForm.name" clearable @clear="nameClear" placeholder="请输入隔离人姓名" size="small"
          style="width: 240px" maxlength="20">
          <el-button slot="append" size="small" :icon="loadingSearch ? 'el-icon-loading' : 'el-icon-search'"
            @click="searchClick">
          </el-button>
        </el-input>
        <el-popover placement="right-start" width="330" trigger="click" style="margin-left: 10px">
          <div>
            <el-form class="popover_from" label-width="70px" style="margin: 20px 0">
              <el-form-item size="small" label="状态：">
                <el-select v-model="searchForm.isolationStatus" placeholder="请选择隔离状态" size="small" style="width: 250px">
                  <el-option v-for="item in isolateStatusArr" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item size="small" label="类型：">
                <el-select v-model="searchForm.isolationType" clearable placeholder="请选择隔离类型" size="small"
                  style="width: 250px">
                  <el-option v-for="item in isolationTypeArr" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item size="small" label="单位：">
                  <el-select v-model="searchForm.orgId" filterable placeholder="请选择单位" style="width: 250px" clearable
                    @change="changeOrgId" @visible-change="enterpriseChange" v-loadMore="loadMore">
                    <el-option v-for="item in orgList" :key="item.id" :label="item.orgName" :value="item.id">
                    </el-option>
                    <div class="div_box" v-loading="loading" :style="loading ? '' : 'display: none;'"> </div>
                  </el-select>
              </el-form-item>
              <el-form-item size="small" label="建筑物" prop="buildId">
                  <el-select v-model="searchForm.buildId" filterable placeholder="请选择所属建筑" style="width: 250px" clearable
                    @change="changeBuild" v-loadMore="loadMore2">
                    <el-option v-for="item in buildList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                    <div class="div_box" v-loading="loading2" :style="loading2 ? '' : 'display: none;'"> </div>
                  </el-select>
              </el-form-item>
              <el-form-item size="small" label="楼层" prop="floorId">
                <el-select v-model="searchForm.floorId" filterable placeholder="请选择所属楼层" style="width: 250px" clearable
                  @change="changeFloor" v-loadMore="loadMore3">
                  <el-option v-for="item in floorList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                  <div class="div_box" v-loading="loading3" :style="loading3 ? '' : 'display: none;'"> </div>
                </el-select>
              </el-form-item>
              <el-form-item size="small" label="安装点" prop="spaceId">
                <el-select v-model="searchForm.spaceId" filterable placeholder="请选择安装点" style="width: 250px" clearable v-loadMore="loadMore4">
                  <el-option v-for="item in spaceList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                  <div class="div_box" v-loading="loading4" :style="loading4 ? '' : 'display: none;'"> </div>
                </el-select>
              </el-form-item>
            </el-form>
            <div class="popover_footer">
              <el-button type="info" size="mini" @click="resetClick" :loading="loadingReset" icon="el-icon-refresh"
                class="reset-button">重置</el-button>
              <el-button type="primary" size="mini" @click="searchClick1" icon="el-icon-search" :loading="loadingSearch1"
              class="search-button">搜索</el-button>
          </div>
        </div>
        <el-button style="margin-left: 15px" size="small" slot="reference">高级搜索<i class="el-icon-search"
            style="margin-left: 5px; font-size: 12px"></i></el-button>
      </el-popover>
    </div>

    <div class="header_right">
      <el-button size="small" icon="el-icon-plus" style="margin-left: 15px" @click="addClick">新增</el-button>
      <!-- <el-button size="medium" style="margin-left: 15px" @click="onExport"
                >导出</el-button
              >
              <el-dropdown @command="onDropdown">
                <el-button size="medium" style="margin-left: 15px">
                  批量操作<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="remove">批量解除隔离</el-dropdown-item>
                  <el-dropdown-item command="del">批量删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown> -->
      </div>
    </div>
    <div class="content">
      <el-table :data="dataList" height="calc(100% - 65px)" class="table" ref="refTable" row-key="id"
        :header-cell-style="headerStyle" @selection-change="batchSelect">
        <el-table-column align="center" type="selection" :reserve-selection="true" width="60"></el-table-column>
        <el-table-column align="center" label="序号" type="index" width="80">
        </el-table-column>
        <el-table-column align="center" prop="spaceName" label="安装点" min-width="100" show-overflow-tooltip>
        </el-table-column>
        <el-table-column align="center" prop="isolationStatus" label="隔离状态" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isolationStatus == 1" type="danger">待隔离</el-tag>
            <el-tag v-if="scope.row.isolationStatus == 2" type="success">隔离中</el-tag>
            <el-tag v-else-if="scope.row.isolationStatus == 3" type="warning">解除隔离</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="isolationType" label="隔离类型" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isolationType == 1" type="warning">居家隔离</el-tag>
            <el-tag v-if="scope.row.isolationType == 2" type="success">酒店隔离</el-tag>
            <el-tag v-if="scope.row.isolationType == 3" type="danger">定点医院隔离</el-tag>
            <el-tag v-else-if="scope.row.isolationType == 4" type="info">其他隔离</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="isolationType" label="隔离因素" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isolationType == 1" type="warning">密接</el-tag>
            <el-tag v-if="scope.row.isolationType == 2" type="success">次密接</el-tag>
            <el-tag v-if="scope.row.isolationType == 3" type="danger">确诊</el-tag>
            <el-tag v-if="scope.row.isolationType == 4" type="info">其他因素</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="隔离开始时间" min-width="145" show-overflow-tooltip>
        </el-table-column>
        <el-table-column align="center" prop="remainingDays" label="隔离剩余天数" min-width="90" show-overflow-tooltip>
        </el-table-column>
        <el-table-column align="center" prop="name" label="隔离人" min-width="100" show-overflow-tooltip>
        </el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="bottom">
              <i @click="editClick(scope.row)" class="iconfont tianxie tableBtnIcon"></i>
            </el-tooltip>
            <el-tooltip v-if="scope.row.isolationStatus == 2" class="item" effect="dark" content="解除隔离"
              placement="bottom">
              <i @click="relieveClick(scope.row)" class="iconfont de-isolation tableBtnIcon"></i>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
              <i @click="delClick(scope.row)" class="iconfont shanchu tableBtnIcon"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page.sync="page" :limit.sync="limit" :total="total" @pagination="getDataList" class="pagination">
      </pagination>
    </div>
    <isolation-add ref="refAdd"></isolation-add>
    <isolation-edit ref="refEdit" :editData="editData"></isolation-edit>
  </div>
</template>

<script>
import {
  reqIsolationPersonToday,
  reqIsolationPersonYesterday,
  reqIsolationPersonList,
  reqIsolationPersonDel,
  reqIsolationPersonRelieve,
} from "@api/api.js";
import tableStyleMixin from "@mixin/table-style";
import exportMixin from "./mixins/export.js";
import advanceSearchMixin from "./mixins/advanceSearch.js";
import Pagination from "@components/Pagination.vue";
import TreeSelSearch from "@components/TreeSelSearch.vue";
import IsolationAdd from "./components/IsolationAdd.vue";
import IsolationEdit from "./components/IsolationEdit.vue";
import { mapState } from "vuex";
import loadMore from "@mixin/loadMore";

export default {
  name: "",
  components: {
    Pagination,
    IsolationAdd,
    IsolationEdit,
    TreeSelSearch,
  },
  mixins: [tableStyleMixin, exportMixin, advanceSearchMixin,loadMore],
  data() {
    return {
      // 今日统计
      isolationPersonTodayCount: {
        inIsolation: 0,
        sumIsolation: 0,
        disIsolation: 0,
      },
      // 昨日统计
      isolationPersonYesterdayCount: {
        inIsolation: 0,
        sumIsolation: 0,
        disIsolation: 0,
      },
      dataList: [],
      page: 1,
      limit: 10,
      total: 0,
      searchForm: {
        name: "",
        orgId: "",
        parkId: "",
        buildId: "",
        floorId: "",
        spaceId: "",
        isolationType: "",
        isolationStatus: "2", //必填
      },

      loadingSearch: false,
      editData: {}, //编辑数据
      batchDataList: [], //批量操作勾选的数据
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  mounted() {
    this.getIsolationPersonToday();
    this.getIsolationPersonYesterday();
    this.getDataList();
  },
  methods: {
    // 获取今日统计
    async getIsolationPersonToday() {
      let params = {
        userId: this.user.userId,
        roleName: this.user.roleName,
      };
      const res = await reqIsolationPersonToday(params);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      let { inIsolation, sumIsolation, disIsolation } = res.data.rows;
      this.isolationPersonTodayCount.inIsolation = inIsolation;
      this.isolationPersonTodayCount.sumIsolation = sumIsolation;
      this.isolationPersonTodayCount.disIsolation = disIsolation;
    },
    // 获取昨日统计
    async getIsolationPersonYesterday() {
      let params = {
        userId: this.user.userId,
        roleName: this.user.roleName,
      };
      const res = await reqIsolationPersonYesterday(params);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      let { inIsolation, sumIsolation, disIsolation } = res.data.rows;
      this.isolationPersonYesterdayCount.inIsolation = inIsolation;
      this.isolationPersonYesterdayCount.sumIsolation = sumIsolation;
      this.isolationPersonYesterdayCount.disIsolation = disIsolation;
    },
    // 获取隔离人列表
    async getDataList() {
      let {
        name,
        isolationStatus,
        isolationType,
        orgId,
        floorId,
        buildId,
        spaceId,
      } = this.searchForm;
      spaceId = spaceId ? spaceId : floorId ? floorId : buildId;
      let params = {
        userId: this.user.userId,
        roleName: this.user.roleName,
        name,
        isolationStatus,
        isolationType,
        orgId,
        spaceId,
      };
      const res = await reqIsolationPersonList(this.page, this.limit, params);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.dataList = res.data.rows;
      this.total = res.data.total;
    },
    getInitDataList() {
      this.page = 1;
      this.limit = 10;
      this.getDataList();
    },
    // 搜索
    async searchClick() {
      this.page = 1;
      this.limit = 10;
      this.loadingSearch = true;
      await this.getDataList();
      this.loadingSearch = false;
    },
    // 清除请求
    nameClear() {
      this.getInitDataList();
    },
    // 新增
    addClick() {
      this.$refs.refAdd.dialogVisible = true;
      this.$refs.refAdd.getOrgList();
    },
    // 编辑
    editClick(row) {
      this.editData = JSON.parse(JSON.stringify(row));
      this.$refs.refEdit.dialogVisible = true;
    },
    // 清除editData
    clearEditData() {
      this.editData = {};
    },
    // 解除隔离
    async relieveClick(row) {
      const result = await this.$confirm(
        `此操作将解除${row.name}的隔离状态，是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => { });
      if (result !== "confirm") return;
      const res = await reqIsolationPersonRelieve(row.id);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.$modal.notifySuccess("成功");
      this.getInitDataList();
    },
    // 删除
    async delClick(row) {
      const result = await this.$confirm(
        `此操作将永久删除隔离人${row.name}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => { });
      if (result !== "confirm") return;
      const res = await reqIsolationPersonDel(row.id);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.$modal.notifySuccess("删除隔离人成功");
      this.getInitDataList();
    },
    // 点击复选框触发，可跨分页选择
    batchSelect(arr) {
      this.batchDataList = arr;
    },
    // 清空复选框√√
    clearSelections() {
      this.$refs.refTable.clearSelection();
    },
    // 更多操作回调事件
    onDropdown(e) {
      let { batchDataList } = this;
      if (batchDataList.length <= 0)
        return this.$modal.notifyWarning("请先勾选需要删除的设备");
      return this.$modal.notifyWarning("功能暂未开启，敬请期待！");
      // console.log(e);
      switch (e) {
        case "remove":
          // console.log("解除");
          break;
        case "del":
          // console.log("删除");
          break;
      }
    },
  },
};
</script>

<style scoped lang="less">
@import url("~@views/style/tablePage.less");

.common-container {

  // background-color: transparent;
  .header {
    background-color: @primaryHeaderBgcolor;
  }
}

.statistics {
  border-radius: 6px;
  // height: 20%;
  min-height: 130px;
  width: 100%;
  margin-bottom: 18px;
  color: @primaryThemeTxtColor;
  display: flex;
  justify-content: space-between;
  padding: 12px 20px 0 20px;
  box-sizing: border-box;

  .item {
    box-sizing: border-box;
    // border-radius: 6px;
    width: 46%;
    background: @primarywarn;
    box-shadow: 0 4px 24px 0 ~"rgb(34 41 47 / 10%)";
    display: flex;
    align-items: center;
    justify-content: space-around;

    .citem {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;

      .number {
        font-size: 20px;
        margin-bottom: 10px;
        width: 60px;
        height: 60px;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .first {
        color: rgb(53, 163, 241);
      }

      .second {
        color: rgb(250, 156, 149);
      }

      .third {
        color: rgb(140, 219, 124);
      }

      .title {
        font-size: 13px;
        color: @primaryThemeTxtColor;
      }
    }

    // .citem:first-of-type .first {
    //   background: url("../../assets/images/Group 1436.png") no-repeat;
    //   background-size: 100%;
    // }
    //  .citem:nth-of-type(2) .first {
    //   background: url("../../assets/images/Group 1435.png") no-repeat;
    //   background-size: 100%;
    // }
    //  .citem:last-of-type .first {
    //   background: url("../../assets/images/Group 1434.png") no-repeat;
    //   background-size: 100%;
    // }
    // .citem:first-of-type .second {
    //   background: url("../../assets/images/Group 1436.png") no-repeat;
    //   background-size: 100%;
    // }
    //  .citem:nth-of-type(2) .second {
    //   background: url("../../assets/images/Group 1435.png") no-repeat;
    //   background-size: 100%;
    // }
    //  .citem:last-of-type .second {
    //   background: url("../../assets/images/Group 1434.png") no-repeat;
    //   background-size: 100%;
    // }
  }
}

.popover_from {
  ::v-deep .el-input__inner {
    background-color: #fff !important;
  }
}

.popover_footer {
  width: 100%;
  display: flex;
  justify-content: center;
}

.content {
  height: calc(100% - 218px) !important;
}

::v-deep .el-button--small {
  background: #7367f0;
  color: #fff;
  border: none;
}

// 下拉框加载开始
.div_box {
  height: 276px;
  position: absolute;
  top: -1px;
  left: -1px;
  width: 100%;
  z-index: 2016;
  // background: red;
  // opacity: 0.5;
}

::v-deep .el-loading-mask {
  border-radius: 4px;
}

// 下拉框加载结束
</style>
